{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n==========================\nSGD: convex loss functions\n==========================\n\nA plot that compares the various convex loss functions supported by\n:class:`sklearn.linear_model.SGDClassifier` .\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(__doc__)\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n\ndef modified_huber_loss(y_true, y_pred):\n    z = y_pred * y_true\n    loss = -4 * z\n    loss[z >= -1] = (1 - z[z >= -1]) ** 2\n    loss[z >= 1.] = 0\n    return loss\n\n\nxmin, xmax = -4, 4\nxx = np.linspace(xmin, xmax, 100)\nlw = 2\nplt.plot([xmin, 0, 0, xmax], [1, 1, 0, 0], color='gold', lw=lw,\n         label=\"Zero-one loss\")\nplt.plot(xx, np.where(xx < 1, 1 - xx, 0), color='teal', lw=lw,\n         label=\"Hinge loss\")\nplt.plot(xx, -np.minimum(xx, 0), color='yellowgreen', lw=lw,\n         label=\"Perceptron loss\")\nplt.plot(xx, np.log2(1 + np.exp(-xx)), color='cornflowerblue', lw=lw,\n         label=\"Log loss\")\nplt.plot(xx, np.where(xx < 1, 1 - xx, 0) ** 2, color='orange', lw=lw,\n         label=\"Squared hinge loss\")\nplt.plot(xx, modified_huber_loss(xx, 1), color='darkorchid', lw=lw,\n         linestyle='--', label=\"Modified Huber loss\")\nplt.ylim((0, 8))\nplt.legend(loc=\"upper right\")\nplt.xlabel(r\"Decision function $f(x)$\")\nplt.ylabel(\"$L(y=1, f(x))$\")\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}